package com.online.shopping.controller;

import com.online.shopping.entity.PageResult;
import com.online.shopping.entity.Result;
import com.online.shopping.pojo.TbBrand;
import com.online.shopping.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/brand-Ms")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @RequestMapping("/findAll")
    public List<TbBrand> findAllBrands() {
        return brandService.findAll();
    }
    @RequestMapping("/findPage")
    public PageResult findPage(int pageNum, int pageSize) {
        return brandService.findPage(pageNum, pageSize);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody TbBrand brand) {
        Result result = new Result();
        try {
            this.brandService.addBrand(brand);
            result.setSuccess(true);
            result.setMessage("添加品牌信息成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("添加品牌信息失败");
        }
        return result;
    }
    @RequestMapping("/findBrandDetail")
    public TbBrand findBrandDetail(Long id) {
        return brandService.findOne(id);
    }
    @RequestMapping("/updateBrand")
    public Result updateBrand(@RequestBody TbBrand brand) {
        Result result = new Result();
        try {
            this.brandService.updateBrands(brand);
            result.setSuccess(true);
            result.setMessage("修改品牌信息成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("修改品牌信息失败");
        }
        return result;
    }
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        Result result = new Result();
        try {
            this.brandService.deleteBrand(ids);
            result.setSuccess(true);
            result.setMessage("删除品牌信息成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("删除品牌信息失败");
        }
        return result;
    }
}